package com.cst.intercepterdemo.chain.expression;

import java.util.ArrayList;
import java.util.List;

/**
 * <ul>
 * <li>文件名称 : com.cst.intercepterdemo.chain.expression.AbstractExpression
 * </li>
 * <li>版权所有 : 版权所有(C)2016-2020</li>
 * <li>公 司 : CST</li>
 * <li>创建时间 : 2016/12/5</li>
 * <li>修改记录 : </li>
 * </ul>
 * 类说明：表达式实现类
 * 本类型表达式
 *
 * @author Allen
 * @version 1.0
 */
public class DefaultExpression implements IExpression {

    /**
     * 开始位置
     */
    private Integer startIndex;

    /**
     * 结束位置
     */
    private Integer endIndex;

    private List<IExpression> subExpression = null;

    @Override
    public void addSubExpression(IExpression expression){
        if(subExpression==null){
            subExpression = new ArrayList<>();
        }

        subExpression.add(expression);
    }

    @Override
    public Integer getEndIndex() {
        return endIndex;
    }

    @Override
    public void setEndIndex(Integer endIndex) {
        this.endIndex = endIndex;
    }

    @Override
    public Integer getStartIndex() {
        return startIndex;
    }

    @Override
    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    @Override
    public Boolean hasSubExpression(){
        return subExpression==null?false:subExpression.size()==0?false:true;
    }

    /**
     * 截取指定返回的字符并连接为字符串
     * @param expressions 字符数组
     * @return
     */
    @Override
    public String getExpression(char[] expressions){
        StringBuffer sb = new StringBuffer();
        for(int i = this.startIndex ; i < this.endIndex ; i++){
            sb.append(expressions[i]);
        }
        return sb.toString();
    }

    @Override
    public List<IExpression> getSubExpression() {
        return subExpression;
    }
}
